export default {
	namespaced: true,
	state: () => ({
		token: uni.getStorageSync('token') || '',
		userinfo: JSON.parse(uni.getStorageSync('userinfo') || '{}'),
		address: JSON.parse(uni.getStorageSync('address') || '{}'),
		myaddress: JSON.parse(uni.getStorageSync('myaddress') || '[]'),
		redirectInfo: null,
	}),
	mutations: {
		addToMyAddress(state, myaddress) {
			console.log(myaddress);
			const findResult = state.myaddress.find(x => x.buyerName === myaddress.buyerName && x.buyerAddr === myaddress.buyerAddr && x.phone === myaddress.phone)
			if(!findResult) {
				state.myaddress.push(myaddress)
			}
		},
		saveToken(state) {
			uni.setStorageSync('token', state.token)
		},
		saveUserInfoToStorage(state) {
			uni.setStorageSync('userinfo', JSON.stringify(state.userinfo))
		},
		saveAddressToStorage(state) {
			uni.setStorageSync('address', JSON.stringify(state.address))
		},
		saveMyAddressToStorage(state) {
			uni.setStorageSync('myaddress', JSON.stringify(state.myaddress))
		},
		updateToken(state, token) {
			state.token = token
			this.commit('module_user/saveToken')
		},
		updateUserInfo(state, userinfo) {
			state.userinfo = userinfo
			this.commit('module_user/saveUserInfoToStorage')
		},
		updateAddress(state, address) {
			state.address = address
			this.commit('module_user/saveAddressToStorage')
		},
		updateMyAddress(state, myaddress) {
			state.myaddress = myaddress
			this.commit('module_user/saveMyAddressToStorage')
		},
		updateMyAddressState(state, myaddress) {
			const findResult = state.myaddress.find(x => x.buyerName === myaddress.buyerName && x.buyerAddr === myaddress.buyerAddr && x.phone === myaddress.phone)
			state.myaddress.forEach(x => x.state = false)
			if(findResult) {
				findResult.state = myaddress.state
			}
			console.log(findResult);
			state.myaddress = JSON.parse(JSON.stringify(state.myaddress))
			this.commit('module_user/saveMyAddressToStorage')
		},
		removeMyAddress(state, myaddress) {
			state.myaddress = state.myaddress.filter(x => {
				return !(x.buyerName === myaddress.buyerName && x.buyerAddr === myaddress.buyerAddr && x.phone === myaddress.phone)
			})
			this.commit('module_user/saveMyAddressToStorage')
		},
		updateRedirectInfo(state, info) {
			state.redirectInfo = info
		}
	},
	getters: {
		addstr(state) {
			if(!state.address.provinceName) return ''
			return state.address.provinceName + state.address.cityName + state.address.countyName + state.address.detailInfo
		},
		defaultAddress(state) {
			console.log(state.myaddress.find(x => x.state));
			return state.myaddress.find(x => x.state)
		}
	}
}